package com.example.rushbuy.controller;

import com.example.rushbuy.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class RushBuyController {

    @Autowired
    private OrderService orderService;

    @RequestMapping("/toLogin")
    public String login(){
        return "login";
    }

    /**
     * 模拟下单
     * @return
     */
    @RequestMapping("/order")
    public String order(Model model){
//        Integer result = orderService.order(1, 2, 1, 4);
//        if(result != 0){
//            //如果抢到了
//            model.addAttribute("orderId",result);
//            return "pay";
//        }else{
//            return "fail";
//        }
        Integer userId = 2;
        Integer itemId = 1;
        boolean isGet = orderService.order(itemId, userId, 1);
        if(isGet){
            model.addAttribute("userId",userId);
            model.addAttribute("itemId",itemId);
            return "pay";
        }else{
            return "fail";
        }
    }

    @RequestMapping("/payProcess")
    @ResponseBody
    public String payProcess(@RequestParam Integer userId,Integer itemId){
        boolean result = orderService.payProcess(itemId, userId);
        if(result){
            return "paySuccess";
        }else
            return "payFail";
    }
}
